package com.tianqu.tms.dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.tianqu.tms.core.exception.TmsDaoException;
import com.tianqu.tms.dto.AucBidTypeDto;

public class AucBidTypeDaoImp extends TmsDao implements AucBidTypeDao {

	protected AucBidTypeDaoImp() throws TmsDaoException {
		super();
	}

	/**
	 * 查询竞价模式种类
	 */
	@Override
	public AucBidTypeDto getAucBidType(int bidtype) throws TmsDaoException {
		PreparedStatement ps = null;
		ResultSet rs = null;
		AucBidTypeDto dto = new AucBidTypeDto();
		try {
			StringBuffer sb = new StringBuffer();
			sb.append("SELECT ");
			sb.append("	ID,	  ");
			sb.append("	TYPENAME	,");
			sb.append("	FAREAMOUNT	,");
			sb.append("	AUCTIONDEADLINE	,");
			sb.append("	DELAYTIME	,");
			sb.append("	FIRSTEXPETPRICES, ");
			sb.append("	UPDATETIME	,");
			sb.append("	UPDATEUSER	,");
			sb.append(" BIDTYPE  ");
			sb.append(" FROM  ");
			sb.append(TmsTableNames.TMS_AUC_BIDTYPE);
			sb.append(" WHERE ");
			sb.append(" BIDTYPE=?");
			ps = conn.prepareStatement(sb.toString());
			ps.setInt(1, bidtype);
			rs = ps.executeQuery();
			while (rs.next()) {
				dto.setId(rs.getInt("ID"));
				dto.setTypeName(rs.getString("TYPENAME"));
				dto.setFareAmount(rs.getInt("FAREAMOUNT"));
				dto.setAuctionDeadline(rs.getInt("AUCTIONDEADLINE"));
				dto.setDelayTime(rs.getInt("DELAYTIME"));
				dto.setFirstExpetPrices(rs.getInt("FIRSTEXPETPRICES"));
				dto.setBidType(rs.getInt("BIDTYPE"));
			}
		} catch (SQLException e) {
			throw new TmsDaoException(e.toString());
		} finally {
			close(rs, ps);
		}
		return dto;

	}

	/**
	 * 载入所有竞价模式
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List loadAucBidType() throws TmsDaoException {
		PreparedStatement ps = null;
		ResultSet rs = null;
		List aucBidTypeList = new ArrayList();
		
		StringBuffer sql = new StringBuffer("");
		sql.append("SELECT ");
		sql.append(" ID,");
		sql.append(" TYPENAME,");
		sql.append(" FAREAMOUNT,");
		sql.append(" AUCTIONDEADLINE,");
		sql.append(" DELAYTIME,");
		sql.append(" FIRSTEXPETPRICES,");
		sql.append(" BIDTYPE,");
		sql.append(" UPDATETIME");
		sql.append(" FROM  ");
		sql.append(TmsTableNames.TMS_AUC_BIDTYPE);
		try {
			ps = conn.prepareStatement(sql.toString());
			rs = ps.executeQuery();
			AucBidTypeDto aucBidTypeDto = null;
			while (rs.next()){
				aucBidTypeDto = new AucBidTypeDto();
				aucBidTypeDto.setId(rs.getInt("id"));
				aucBidTypeDto.setTypeName(rs.getString("typeName"));
				aucBidTypeDto.setFareAmount(rs.getInt("fareAmount"));
				aucBidTypeDto.setAuctionDeadline(rs.getInt("auctionDeadline"));
				aucBidTypeDto.setDelayTime(rs.getInt("delayTime"));
				aucBidTypeDto.setFirstExpetPrices(rs.getInt("firstExpetPrices"));
				aucBidTypeDto.setBidType(rs.getInt("bidType"));
				aucBidTypeDto.setUpdateTime(rs.getTimestamp("updateTime"));
				aucBidTypeList.add(aucBidTypeDto);
			}
			return aucBidTypeList;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			throw new TmsDaoException(e.toString());
		}finally {
			super.close(rs, ps);
		}
		
	}

	/**
	 * 竞价模式添加
	 */
	@Override
	public void insertAucBidType(AucBidTypeDto dto) throws TmsDaoException {
		PreparedStatement ps = null;
		ResultSet rs = null;
	
		try {
			StringBuffer sql = new StringBuffer("INSERT INTO ");
			sql.append(TmsTableNames.TMS_AUC_BIDTYPE);
			sql.append("(");
			sql.append(" TYPENAME, ");
			sql.append(" FAREAMOUNT, ");
			sql.append(" AUCTIONDEADLINE, ");
			sql.append(" DELAYTIME, ");
			sql.append(" FIRSTEXPETPRICES, ");
			sql.append(" BIDTYPE, ");
			sql.append(" UPDATETIME, ");
			sql.append(" UPDATEUSER ");
			sql.append("  ) VALUES ( ");
			sql.append(" ?,?,?,?,?,?,SYSDATE(),? )");
			ps = conn.prepareStatement(sql.toString());
			
			ps.setString(1, dto.getTypeName());
			ps.setInt(2, dto.getFareAmount());
			ps.setInt(3, dto.getAuctionDeadline());
			ps.setInt(4, dto.getDelayTime());
			ps.setInt(5, dto.getFirstExpetPrices());
			ps.setInt(6, dto.getBidType());
			ps.setInt(7, dto.getUpdateUser());
			
			ps.executeUpdate();
		} catch (SQLException e) {
			throw new TmsDaoException(e.toString());
		} finally {
			super.close(rs, ps);
		}
		
	}

	
	/**
	 * 判断是否有相同的竞价模式（根据模式名称）
	 */
	@Override
	public boolean typeNameOnly(int bidType) throws TmsDaoException {
		PreparedStatement ps = null;
		ResultSet rs = null;
		
		
		try {
			StringBuffer sql = new StringBuffer("");
			sql.append("SELECT ");
			sql.append(" ID");
			sql.append(" FROM  ");
			sql.append(TmsTableNames.TMS_AUC_BIDTYPE);
			sql.append(" WHERE  ");
			sql.append(" BIDTYPE=? ");
			ps = conn.prepareStatement(sql.toString());
			
			ps.setInt(1, bidType);
			
			rs = ps.executeQuery();
			if(rs.next()){
				return false;
			}
		} catch (SQLException e) {
			throw new TmsDaoException(e.toString());
		}finally {
			super.close(rs, ps);
		}
		
		
		return true;
	}

	/**
	 * 根据id载入竞价模式
	 */
	@Override
	public AucBidTypeDto getAucBidTypeById(long id) throws TmsDaoException{
		PreparedStatement ps = null;
		ResultSet rs = null;
		AucBidTypeDto dto = new AucBidTypeDto();
		try {
			StringBuffer sb = new StringBuffer();
			sb.append("SELECT ");
			sb.append("	ID,	  ");
			sb.append("	TYPENAME	,");
			sb.append("	FAREAMOUNT	,");
			sb.append("	AUCTIONDEADLINE	,");
			sb.append("	DELAYTIME	,");
			sb.append("	FIRSTEXPETPRICES, ");
			sb.append("	UPDATETIME	,");
			sb.append("	UPDATEUSER	,");
			sb.append(" BIDTYPE  ");
			sb.append(" FROM  ");
			sb.append(TmsTableNames.TMS_AUC_BIDTYPE);
			sb.append(" WHERE ");
			sb.append(" ID=?");
			ps = conn.prepareStatement(sb.toString());
			ps.setLong(1, id);
			rs = ps.executeQuery();
			while (rs.next()) {
				dto.setId(rs.getInt("ID"));
				dto.setTypeName(rs.getString("TYPENAME"));
				dto.setFareAmount(rs.getInt("FAREAMOUNT"));
				dto.setAuctionDeadline(rs.getInt("AUCTIONDEADLINE"));
				dto.setDelayTime(rs.getInt("DELAYTIME"));
				dto.setFirstExpetPrices(rs.getInt("FIRSTEXPETPRICES"));
				dto.setBidType(rs.getInt("BIDTYPE"));
			}
		} catch (SQLException e) {
			throw new TmsDaoException(e.toString());
		} finally {
			close(rs, ps);
		}
		return dto;
	}

	/**
	 * 竞价模式修改
	 */
	@Override
	public void updateAucBidType(AucBidTypeDto dto) throws TmsDaoException {
		PreparedStatement ps = null;
		ResultSet rs = null;
		StringBuffer sql = new StringBuffer("");
		try {
			sql.append("UPDATE ");
			sql.append(TmsTableNames.TMS_AUC_BIDTYPE);
			sql.append(" SET ");
			sql.append(" TYPENAME=?, ");
			sql.append(" FAREAMOUNT=?, ");
			sql.append(" AUCTIONDEADLINE=?, ");
			sql.append(" DELAYTIME=?, ");
			sql.append(" FIRSTEXPETPRICES=?, ");
			sql.append(" BIDTYPE=?, ");
			sql.append(" UPDATETIME=SYSDATE(), ");
			sql.append(" UPDATEUSER=? ");
			sql.append(" WHERE ");
			sql.append(" ID=? ");
			ps = conn.prepareStatement(sql.toString());
			
			ps.setString(1, dto.getTypeName());
			ps.setInt(2, dto.getFareAmount());
			ps.setInt(3, dto.getAuctionDeadline());
			ps.setInt(4, dto.getDelayTime());
			ps.setInt(5, dto.getFirstExpetPrices());
			ps.setInt(6, dto.getBidType());
			ps.setInt(7, dto.getUpdateUser());
			ps.setLong(8, dto.getId());
			ps.executeUpdate();
		} catch (SQLException e) {
			throw new TmsDaoException(e.toString());
		} finally {
			close(rs, ps);
		}
	}

	/**
	 * 竞价模式删除
	 */
	@Override
	public void deleteAucBidType(long id) throws TmsDaoException {
		PreparedStatement ps = null;
		ResultSet rs = null;
		
		try {
			StringBuffer sql = new StringBuffer("");
			sql.append("DELETE ");
			sql.append("FROM ");
			sql.append(TmsTableNames.TMS_AUC_BIDTYPE);
			sql.append(" WHERE ");
			sql.append(" ID=? ");
			ps = conn.prepareStatement(sql.toString());
			
			ps.setLong(1, id);
			ps.executeUpdate();
		} catch (SQLException e) {
			throw new TmsDaoException(e.toString());
		} finally {
			close(rs, ps);
		}
	}

}
